package com.hippo.core.tuya

import android.util.Log
import com.hippo.core.state.DeviceValue
import com.hippo.core.utils.UiHandler
import com.thingclips.smart.android.device.bean.DeviceDpInfoBean
import com.thingclips.smart.android.device.builder.ThingTimerBuilder
import com.thingclips.smart.android.device.enums.TimerDeviceTypeEnum
import com.thingclips.smart.home.sdk.ThingHomeSdk
import com.thingclips.smart.home.sdk.constant.TimerUpdateEnum
import com.thingclips.smart.sdk.api.IResultCallback
import com.thingclips.smart.sdk.api.IThingDataCallback
import com.thingclips.smart.sdk.bean.DeviceBean
import com.thingclips.smart.sdk.bean.Timer
import com.thingclips.smart.sdk.bean.TimerTask


object ThingControl {

    const val TAG = "ThingControl"

    var devId: String? = null

    private val workStatusMapper = mapOf(
        "smart_clean" to "Auto Cleaning",
        "spot_clean" to "Edge Cleaning",
        "cleaning" to "Manual Cleaning",
        "standby" to "Standby"
    )


    fun getDeviceBean(): DeviceBean? {
        return ThingHomeSdk.getDataInstance().getDeviceBean(devId)
    }


    fun parseWorkStatusName(status: String?): String? {
        return workStatusMapper[status] ?: status
    }


    fun addTimer(
        devId:String,
        status: Boolean,
        actions:String,
        loops:String,
        callback:(Boolean)->Unit
    ) {
        Log.i(TAG, "addTimer: ")

        val builder = ThingTimerBuilder.Builder()
            .devId(devId)
            .deviceType(TimerDeviceTypeEnum.DEVICE)
            .actions(actions)
            .loops(loops)
            .status(if(status) 1 else 0)
            .appPush(true)
            .build()

        ThingHomeSdk.getTimerInstance().addTimer(builder, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                Log.i(TAG, "code:$code ,error: $error")
                UiHandler.showToast(error)
            }

            override fun onSuccess() {
                callback(true)
            }
        })
    }
    fun updateTimer(
        timerId:String,
        devId:String,
        dps:String,
        loops:String,
        status: Boolean,
        callback:(Boolean)->Unit
    ) {
        Log.i(TAG, "updateTimer: sss")

        val builder = ThingTimerBuilder.Builder()
            .timerId(timerId.toLong())
            .devId(devId)
            .deviceType(TimerDeviceTypeEnum.DEVICE)
            .actions(dps)
            .loops(loops)
            .status(if(status) 1 else 0)
            .appPush(true)
            .build()

        ThingHomeSdk.getTimerInstance().updateTimer(builder, object : IResultCallback {
            override fun onError(code: String?, error: String?) {
                Log.i(TAG, "code:$code ,error: $error")
                UiHandler.showToast(error)
            }

            override fun onSuccess() {
                Log.i(TAG, "updateTimer sss: onSuccess")
                callback(true)
            }
        })
    }



    fun getTimerList(devId:String,callback:(Timer?)->Unit){
        ThingHomeSdk.getTimerInstance().getTimerList(
            null,
            devId,
            TimerDeviceTypeEnum.DEVICE,
            object : IThingDataCallback<TimerTask?> {
                override fun onSuccess(timerTask: TimerTask?) {
                    callback(timerTask?.timerList?.firstOrNull())
                }

                override fun onError(errorCode: String, errorMessage: String) {
                    Log.i(TAG, "code:$errorCode ,error: $errorMessage")
                    UiHandler.showToast(errorMessage)
                }
            })

    }


    fun getDeviceDpInfo(devId: String, successCallback: (ArrayList<DeviceDpInfoBean>?) -> Unit) {
        ThingHomeSdk.getDeviceMultiControlInstance()
            .getDeviceDpInfoList(devId, object :
                IThingDataCallback<ArrayList<DeviceDpInfoBean>> {
                override fun onSuccess(result: ArrayList<DeviceDpInfoBean>?) {
                    if (result != null) {
                        successCallback(result)
                    }
                }

                override fun onError(errorCode: String?, errorMessage: String?) {
                    Log.i(TAG, "onError: errorCode = $errorCode errorMessage = $errorMessage")
                    UiHandler.showToast(errorMessage)
                }
            })
    }


    fun updateCategoryTimerStatus(devId: String,isOpen: Boolean){
        
        val update = if(isOpen){
            TimerUpdateEnum.OPEN
        }else{
            TimerUpdateEnum.CLOSE
        }
        
        ThingHomeSdk.getTimerInstance().updateCategoryTimerStatus(
            null,
            devId,
            TimerDeviceTypeEnum.DEVICE,
            update,
            object : IResultCallback {
                override fun onSuccess() {
                    
                }

                override fun onError(errorCode: String, errorMsg: String) {
                    Log.i(TAG, "onError: $errorMsg")
                }
            })

    }

    fun getDps(devId: String): MutableMap<String, Any>? {
        return ThingHomeSdk.getDataInstance().getDps(devId)
    }


    fun getDeviceValue(devId: String): DeviceValue {
        val dps = getDps(devId)
        return if (dps != null) {
            DeviceValue(
                switch = dps[DpType.SWITCH.dpId] as Boolean?,
                switch_go = dps[DpType.SWITCH_GO.dpId] as Boolean?,
                mode = dps[DpType.MODE.dpId] as String?,
                direction_control = dps[DpType.DIRECTION_CONTROL.dpId] as String?,
                status = dps[DpType.STATUS.dpId] as String?,
                battery_percentage = dps[DpType.BATTERY_PERCENTAGE.dpId] as Int?,
                suction = dps[DpType.SUCTION.dpId] as String?,
                clean_area = dps[DpType.CLEAN_AREA.dpId] as Int?,
                clean_time = dps[DpType.CLEAN_TIME.dpId] as Int?,
//                fault = dps[DpType.FAULT.dpId] as String?,
                water_quality_ph = dps[DpType.WATER_QUALITY_PH.dpId] as Int?,
                water_quality_salt = dps[DpType.WATER_QUALITY_SALT.dpId] as Int?,
                water_quality_oxygen = dps[DpType.WATER_QUALITY_OXYGEN.dpId] as Int?,
                water_quality_temperature = dps[DpType.WATER_QUALITY_TEMPERATURE.dpId] as Int?,
                water_quality_ec = dps[DpType.WATER_QUALITY_EC.dpId] as Int?,
                sensor_life = dps[DpType.SENSOR_LIFE.dpId] as Int?,
                slide_life = dps[DpType.SLIDE_LIFE.dpId] as Int?,
                appointment_notice = dps[DpType.APPOINTMENT_NOTICE.dpId] as Boolean?
            )
        } else {
            DeviceValue()
        }
    }

    fun Int.celsiusToFahrenheit(): Int {
        return (this * 9 / 5) + 32
    }

}


